package shajie.shajun.mmhf.Control;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.SharedPreferences;
import android.databinding.DataBindingUtil;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;

import java.text.SimpleDateFormat;

import shajie.shajun.mmhf.Public_Data;
import shajie.shajun.mmhf.R;
import shajie.shajun.mmhf.View.MainActivity;
import shajie.shajun.mmhf.databinding.FragmentLoginBinding;


/**
 * Created by admin on 2018/2/25.
 */

public class LoginView extends FrameLayout implements View.OnClickListener{
    public FragmentLoginBinding mBinding;
    private Context mContext;
    private  SharedPreferences.Editor editor;
    private MainActivity mMainActivity;
    public LoginView(@Nullable Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext=context;
        this.mMainActivity= ((MainActivity)mContext);
        mBinding=DataBindingUtil.inflate(LayoutInflater.from(context),R.layout.fragment_login,null,false);
        addView(mBinding.getRoot());
        initView();
    }
    private void initView(){
        mBinding.setOnclicklistener(this);
       // checkoldaccount();
    }
//    private void checkoldaccount(){
//        SharedPreferences pref =mContext.getSharedPreferences("MMHF",
//                MODE_PRIVATE);
//        final String password = pref.getString("password", "");
//        Handler handler = new Handler(Looper.getMainLooper());
//        handler.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                final User user = BmobUser.getCurrentUser(User.class);
//                if (user == null) {
//                    mBinding.loginUI.setVisibility(View.VISIBLE);;
//                } else {
//                    Log.e("runloding", "runloding" );
//                    mMainActivity.mainBinding.loading.setVisibility(View.VISIBLE);
//                    user.setPassword(password);
//                    user.loginObservable(User.class)
//                            .subscribe(new Action1<User>() {
//                                @Override
//                                public void call(User user) {
//                                    mMainActivity.mainBinding.loading.setVisibility(View.GONE);
////                                    Bmobdata.modifyInstallationUser(user, new Bmobdata.Bmob_Modify() {
////                                        @Override
////                                        public void faile(String s) {
////                                            toast(s);
////                                        }
////                                    });
//                                    mBinding.loginUI.setVisibility(View.GONE);
//                                    initrMoney(user);
//                                    Public_Data.p_user = user;
//                                    upuserdata();
//                                    mMainActivity.initbottomNavigationView();
//                                    opendoor();
//                                }
//                            }, new Action1<Throwable>() {
//                                @Override
//                                public void call(Throwable throwable) {
//                                    mBinding.loginUI.setVisibility(View.VISIBLE);
//                                    mBinding.account.setText(user.getAccount());
//                                    mBinding.password.setText(password);
//                                    mMainActivity.mainBinding.loading.setVisibility(View.GONE);
//                                    toast("登录异常：" + throwable.getMessage());
//                                }
//                            });
//                }
//            }
//        }, 0);
//    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login:
//                if (TextUtils.isEmpty(mBinding.account.getText())) {
//                    mBinding.tlAccount.setError("账户不能为空！");
//                    return;
//                } else if (TextUtils.isEmpty(mBinding.password.getText())) {
//                    mBinding.tlPassword.setError("密码不能为空！");
//                    return;
//                }
//                User user = new User();
//                user.setUsername(mBinding.account.getText().toString());
//                user.setPassword(mBinding.password.getText().toString());
//                //Logger.i(account + "\n" + password);
//                user.login(new SaveListener<User>() {
//                    @Override
//                    public void done(User user, BmobException e) {
//                        if (e == null) {
//                            toast("success");
//                            editor = mContext.getSharedPreferences("MMHF", MODE_PRIVATE).edit();
//                            editor.putString("password", mBinding.password.getText().toString());
//                            editor.commit();
//                            toast("登录成功！");
//                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                            //(12小时制 yyyy-MM-dd HH:mm:ss a)
//                            String date = sdf.format(new java.util.Date());
//                            try {
//                                Public_Data.startdate = sdf.parse(date);
//                            } catch (Exception ee) {
//                                ee.printStackTrace();
//                            }
//                            mBinding.loginUI.setVisibility(View.GONE);
//                            initrMoney(user);
//                            Public_Data.p_user = user;
//                            mMainActivity.initbottomNavigationView();
//                            opendoor();
//                        } else {
//                            toast("登录异常：" + e.getMessage());
//
//                        }
//                    }
//                });
                break;
        }
    }
    private void upuserdata(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//(12小时制 yyyy-MM-dd HH:mm:ss a)
        String date = sdf.format(new java.util.Date());
        try {
            Public_Data.startdate = sdf.parse(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Public_Data.backtohome = true;
    }
    private void opendoor(){
        ObjectAnimator alpha = ObjectAnimator.ofFloat(mBinding.door, "alpha", 1f, 0f);
        alpha.setDuration(1200);
        alpha.start();
    }
    public void closedoor(){
        ObjectAnimator alpha = ObjectAnimator.ofFloat( mBinding.door, "alpha", 0f, 1f);
        alpha.setDuration(200);
        alpha.start();
    }
//    private void initrMoney(User user){
//        mMainActivity.mainBinding.leftmenu.user.setText(user.getAccount()+" - "+user.getJob());
//    }
    private void toast(Object o){
        Toast.makeText(mContext, ""+o.toString(), Toast.LENGTH_SHORT).show();
    }
}
